package com.example.shellshopping.fpage;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.shellshopping.R;
import com.example.shellshopping.goods.GoodsBean;
import com.example.shellshopping.goods.GoodsDetailsActivity;
import com.example.shellshopping.goods.GoodsDetailsBean;
import com.example.shellshopping.utils.BaseFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class HotFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    GridView gv;
    List<GoodsBean.DataBean> data;
    private HotAdapter adapter;
    public HotFragment() {
    }
    public static HotFragment newInstance(String param1, String param2) {
        HotFragment fragment = new HotFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot, container, false);
        gv = view.findViewById(R.id.frag_hot_gv);
        //数据源
        data = new ArrayList<>();
        //适配器
        adapter = new HotAdapter(getContext(),data);
        gv.setAdapter(adapter);
        //联网
        getNetwork(mParam1);
        //设置监听
        gv.setOnItemClickListener(listener);
        return view;
    }

    AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getContext(), GoodsDetailsActivity.class);
            //数据类型转换
            GoodsDetailsBean.DataBean newGoodsDetailBean = new GoodsDetailsBean.DataBean();
            newGoodsDetailBean.setCategoryId(data.get(position).getCategoryId());
            newGoodsDetailBean.setCount(data.get(position).getCount());
            newGoodsDetailBean.setId(data.get(position).getId());
            newGoodsDetailBean.setInfo(data.get(position).getInfo());
            newGoodsDetailBean.setName(data.get(position).getName());
            newGoodsDetailBean.setPhoto(data.get(position).getPhoto());
            newGoodsDetailBean.setPrice(data.get(position).getPrice());
            intent.putExtra("goods",newGoodsDetailBean);
            startActivity(intent);
        }
    };
    @Override
    public void onSuccess(String result){
        super.onSuccess(result);
        //Toast.makeText( getContext(), "查询成功", Toast.LENGTH_SHORT ).show();
        GoodsBean goodsBean = new Gson().fromJson(result,GoodsBean.class);//解析数据
        List<GoodsBean.DataBean> list = goodsBean.getData();//获取网络集合
        data.addAll(list);//将网络数据添加到list view的数据源当中
        adapter.notifyDataSetChanged();//提示更新
    }
}